home *** CD-ROM | disk | FTP | other *** search
- /*****************************************************************************
- * FILE: CMSoundSystem.h
- * AUTHOR: David Hay
- * CREATED: March 9, 1995
- * DESCRIPTION: Interface to the Caveman Sound System.
- *
- * Copyright © 1995-1997 David Hay
- *
- * Permission to use, copy, and distribute this software and its documentation
- * for any purpose is hereby granted without fee, provided that (i) the above
- * copyright notices and this permission notice appear in all copies of the
- * software and related documentation, and (ii) the names of David Hay and
- * Caveman Creations may not be used in any advertising or publicity relating
- * to the software without the specific, prior written permission of David Hay
- *
- * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS,
- * IMPLIED OR OTHERWISE, INCLUDING, WITHOUT LIMITATION, ANY WARRANTY OF
- * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
- * DAVID HAY OR CAVEMAN CREATIONS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
- * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
- * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
- * POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
- * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- *
- *
- * Version History:
- *
- * 1.0 (09/11/95)
- * - Initial release
- *
- * 1.1 (03/05/96)
- * - Incorporated changes by Dario Accornero (dappsoft@mix.it) Thanks
- * Dario for all your work! (look for "// DA on 01/20/96")
- * • added PPC support in the callback routine
- * • added stero Macs support in the channel allocation routines
- * • added double buffer support in order to speed up sound output
- * on all Macs
- * • chaned SoundSystem allocation, in order to lower global vars
- * space memory charge; now, the SoundSystem is accessed thru a
- * pointer, so it is *necessare* to allocate the system before
- * using it, otherwise the code will crash…
- * • added priorities support
- * • added channel volume control (get/set)
- * • CodeWarrior is now supported
- * - Cleaned up the code a bit.
- * - Added some internal routines for code that was getting repeated by
- * both sound and music routines.
- *****************************************************************************/
-
- #ifndef __CMSOUNDSYSTEM__
- #define __CMSOUNDSYSTEM__
-
- #ifndef __SOUND__
- #include <Sound.h>
- #endif
-
- #define kMaxChannels 20 /* The maximum number of channels allowed */
- #define kMaxSounds 50 /* Maximum number of sounds that may be registered */
- #define kMaxMusicLen 128 /* Maximum length of a song sequence. */
- #define kNoSound (-1) /* Reference number for no sound at all */
- #define kSoundResource (-2) /* Reference number of an unregistered sound */
- #define kNoLoop (-1) /* In a music sequence, indicates no looping */
- #define kMusicResType 'MUSL'/* Resource type for a music list */
- #define kAsynchronous true /* Constant to define asychronous play */
- #define kPriorityOverride 1 /* Error code returned when a sound is overridden */
- #define kPriorityDenied 2 /* Error code returned when no channels are free */
-
- // DA on 01/20/96: constants used to get and set output volume on a specific channel
-
- #define kMinVolume 0 /* minimum volume level */
- #define kMaxVolume 256 /* maximum volume level */
-
- /*****************************************************************************
- ** PlaySequence -- Describes a sequence of sounds to play in order to
- ** produce a piece of music.
- **
- ** loopStart - the position in the sequence to jump to after the
- ** sequence as finished.
- ** sequenceLength - the number of sounds to play in the sequence.
- ** sequence[] - the list of sounds to play. This array determines
- ** the order of sounds to play, allowing a sound
- ** to be repeated several times in a piece of music.
- **/
-
- typedef struct PlaySequence
- {
- short loopStart;
- short sequenceLength;
- short sequence[1]; /* Variable sized */
- } PlaySequence, *PlaySequencePtr, **PlaySequenceHandle;
-
-
- /*----------------------- INITIALIZATION/DEALLOCATION -----------------------*/
-
- extern OSErr
- CMSInitSound( short numChannels );
- /*
- * Initialize the sound system.
- *
- * ON ENTRY: 'numChannels' is a number greater than 0
- * ON EXIT: The sound system has been initialized to provide 'numChannels'
- * channels of sound.
- * Any errors are returned.
- *****************************************************************************/
-
- extern void
- CMSDisposeSound( void );
- /*
- * Dispose of any registered sound and internal data used by the sound
- * system. Any sound system functions should not be used after calling
- * this function.
- *
- * ON ENTRY: The sound system has been previously initialized
- * ON EXIT: Any data used by the sound system is disposed of.
- *****************************************************************************/
-
-
- /*---------------------------- CHANNEL MANAGEMENT ---------------------------*/
-
- extern OSErr
- CMSCloseChannel( short channelNum );
- /*
- * Closes sound channel number 'channelNum'. The channel is still allocated,
- * but sound may not be played through it. To reopen the channel, call
- * CMSOpenChannel()
- *
- * ON ENTRY: 'channelNum' is a positive number less than the number of
- * available channels [as passed to CMSInitSound()]
- * The sound system has been initialized
- * ON EXIT: The channel number 'channelNum' has been closed.
- * Any errors are returned.
- *****************************************************************************/
-
- extern OSErr
- CMSCloseAllChannels( void );
- /*
- * Closes all of the open channels. The channels are still allocated, but
- * sound may not be played through them. To reopen a specific channel, call
- * CMSOpenChannel(). To reopen all channels, call CMSOpenAllChannels().
- *
- * ON ENTRY: The sound system has been initialized
- * ON EXIT: All open sound channels have been closed.
- * Any errors are returned.
- *****************************************************************************/
-
- extern OSErr
- CMSOpenChannel( short channelNum );
- /*
- * Opens channel number 'channelNum'. After opening a channel, sound may be
- * played through that sound channel. If the sound channel is alreay open,
- * nothing is done.
- *
- * ON ENTRY: 'channelNum' is a positive number less than the number of
- * available channels [as passed to CMSInitSound()]
- * The sound system has been initialized
- * ON EXIT: The channel, 'channelNum' is opened and ready to play sounds
- * Any errors are returned.
- *****************************************************************************/
-
- extern OSErr
- CMSOpenAllChannels( void );
- /*
- * Opens all currently closed sound channels. After opening a sound channel,
- * sound may be played on that sound channel.
- *
- * ON ENTRY: The sound system has been initialized.
- * ON EXIT: All allocated sound channels have been opened.
- * Any errors are returned.
- *****************************************************************************/
-
- extern OSErr
- CMSStopSound( short channelNum );
- /*
- * Stops any currently playing sounds on sound channel 'channelNum'
- *
- * ON ENTRY: 'channelNum' is a positive number less than the number of
- * available sound channels [as passed to CMSInitSound()]
- * ON EXIT: Any sounds playing on channel, 'channelNum' are stopped. If
- * the sound playing on the sound channel was a sound resource,
- * the resource is unlocked so that it may be purged.
- *****************************************************************************/
-
- extern void
- CMSIdleSoundTask( void );
- /*
- * Handles any housecleaning tasks the sound system may need to perform. It
- * should be called before playing a sound resource.
- *
- * ON ENTRY: The sound system has been initialized
- * ON EXIT: Any routine chores the sound system have been performed.
- *****************************************************************************/
-
- extern OSErr
- CMSGetSoundPlaying( short channelNum, short* refNum );
- /*
- * Get the reference number of the sound currently playing on a sound channel.
- *
- * ON ENTRY: 'channelNum' is a positive number less than the number of
- * available sound channels [as passed to CMSInitSound()]
- * 'refNum' points to location to store the currently playing
- * sound reference number.
- * ON EXIT: The sound playing on the channel, 'channelNum' is returned in
- * the area pointed to by 'refNum'. If no sounds are currently
- * playing, 'kNoSound' is returned in 'refNum'. If the currently
- * playing sound is a sound resource, 'kSoundResource' is returned
- *****************************************************************************/
-
- extern OSErr
- CMSWaitForSilence( short channelNum );
- /*
- * Wait for a sound channel to fall silent. This should not be called on
- * a sound channel that is playing music with a loop as it will never return.
- *
- * ON ENTRY: 'channelNum' is a positive number less than the number of
- * available sound channels [as passed to CMSInitSound()]
- * that is not playing music with a loop.
- * ON EXIT: The channel, 'channelNum' has fallen silent and is no longer
- * producing sound.
- * Any errors are returned.
- *****************************************************************************/
-
-
- /*--------------------------- SOUND DATA MANAGEMENT -------------------------*/
-
- extern OSErr
- CMSRegisterSound( SndListHandle theSound, short* refNum );
- /*
- * Registers a sound with the sound system for later playback.
- *
- * ON ENTRY: 'theSound' is a valid sound handle.
- * 'refNum' points to space to store a sound reference number.
- * ON EXIT: The sound handle 'theSound' has been registered with the
- * sound system. The sound may be played back at a later time
- * by calling CMSPlaySound() with the returned reference number.
- * Any errors are returned.
- *****************************************************************************/
-
- extern OSErr
- CMSRemoveSound( short refNum );
- /*
- * Removes a previously registered sound from the sound system.
- *
- * ON ENTRY: 'refNum' is the reference number of a sound previously
- * registered with the sound system with either
- * CMSRegisterSound() or CMSLoadSound().
- * ON EXIT: The sound referenced by 'refNum' is removed from the sound
- * system and disposed of.
- * Any errors are returned.
- *****************************************************************************/
-
- extern OSErr
- CMSLoadSound( short soundID, short* refNum );
- /*
- * Loads a sound from the current resource file and registers it with the
- * sound system.
- *
- * ON ENTRY: 'soundID' is the resource ID of a valid 'snd ' resource.
- * 'refNum' points to space to store a sound reference number.
- * ON EXIT: The sound resource ('snd ') number 'soundID' is loaded from
- * the current resource file and registered with the sound
- * system.
- * Any error codes are returned.
- *****************************************************************************/
-
- extern OSErr
- CMSPlaySound( short refNum, short channelNum );
- /*
- * Plays a registered sound on a sound channel.
- *
- * ON ENTRY: 'refNum' is the reference number of a sound previously
- * registered with the sound system with either
- * CMSRegisterSound() or CMSLoadSound().
- * 'channelNum' is a positive number less than the number of
- * available sound channels [as passed to CMSInitSound()]
- * ON EXIT: The sound referenced by 'refNum' has begun playing on sound
- * channel, 'channelNum'.
- * Any error codes are returned.
- *****************************************************************************/
-
- extern OSErr
- CMSPlaySoundResource( short resID, short channelNum, Boolean async );
- /*
- * Plays a sound from a resource file. This function is meant to provide
- * the functionality of SndPlay() through the sound system.
- *
- * ON ENTRY: 'resID' is the number of a valid 'snd ' resrouce to play
- * 'channelNum' is a positive number less than the number of
- * available sound channels [as passed to CMSInitSound()]
- * 'async' is a boolean value indicating whether the sound should
- * be played asynchronosly or not.
- * ON EXIT: The sound resource has begun playing on channel, 'channelNum'.
- * If 'async' is false, then CMSPlaySoundResource() will not
- * return until the sound has finished playing in it's entirety.
- * Any error codes are returned.
- *****************************************************************************/
-
-
- /*----------------------------- MUSIC MANAGEMENT ----------------------------*/
-
- extern OSErr
- CMSLoadMusic( short musicID );
- /*
- * Loads the sounds and music sequence data as defined by the music list
- * resource 'musicID'. The music may then be played with a call to
- * CMSPlayMusic().
- *
- * ON ENTRY: 'musicID' is the number of a valid 'MUSL' resource to load.
- * ON EXIT: Loads the music described by the 'MUSL' resource, 'musicID'.
- * Each of the sounds listed in the play sequence is loaded and
- * registered with the sound system. The internal play sequence
- * is set up in preparation for playing music.
- *****************************************************************************/
-
- extern OSErr
- CMSRemoveMusic( void );
- /*
- * Removes the currently loaded piece of music from the sound system. Any
- * sounds associated with playing music are ungregistered and freed.
- *
- * ON ENTRY: The sound system has been initialized
- * ON EXIT: A piece of music previously loaded with CMSLoadMusic() is
- * removed from the system. Any sounds associated with the
- * music are removed from the system and freed. If no music
- * had been previously loaded, control is returned to the caller
- * immediately with no errors.
- *****************************************************************************/
-
- extern OSErr
- CMSPlayMusic( short channelNum );
- /*
- * Begins a piece of music previously loaded with CMSLoadMusic().
- *
- * ON ENTRY: 'channelNum' is a positive number less than the number of
- * available sound channels [as passed to CMSInitSound()]
- * ON EXIT: The music previously loaded has begun to play on sound channel
- * 'channelNum'. The music will continue until it reaches the
- * end of the piece (if there are no loops). Or until the sound
- * on the channel is silenced with a call to CMSStopSound().
- *****************************************************************************/
-
-
- /*----------------------------- UTILITY ROUTINES ----------------------------*/
-
- extern SoundHeaderPtr
- CMSGetSoundHeader( SndListHandle sndHandle );
- /*
- * Obtains a pointer to the sound header in the given sound resource. Because
- * a pointer into the handle is returned, the handle is locked and should not
- * be unlocked until the sound header is no longer needed. A pointer to a
- * sampled sound header is returned even if the sound header is actually an
- * extended sound header or a compressed sound header.
- *
- * ON ENTRY: 'sndHandle' is a valid sound handle.
- * ON EXIT: A pointer to a valid sound header in the sound 'sndHandle' is
- * returned. If there is not a sound header in the given resource,
- * then NULL is returned.
- *****************************************************************************/
-
- extern OSErr
- CMSGetSoundHeaderOffset( SndListHandle soundHandle, long* theOffset );
- /*
- * Traverses a sound resource until it reaches the sound data. It returns
- * the offset in bytes from the beginning of a sound resource to the sound
- * header.
- *
- * ON ENTRY: 'soundHandle' is a valid sound handle.
- * 'theOffset' points to a location to store the header offset.
- * ON EXIT: The offset in the sound handle 'soundHandle' is returned in
- * the space pointed to by 'theOffset'.
- * Any errors are returned.
- *****************************************************************************/
-
-
- /*----------------------------- INQUIRY ROUTINES ----------------------------*/
-
- extern Boolean
- CMSHasNewSoundManager( void );
- /*
- * Determines if the current sound manager is the "new" one. The "new"
- * Sound Manager contains the _SoundDispatch trap and is version 2.0 or
- * greater.
- *
- * ON ENTRY: no preconditions
- * ON EXIT: Returns true if the "new" sound manager is present on the
- * current machine.
- *****************************************************************************/
-
- extern Boolean
- CMSHasMultipleChannels( void );
- /*
- * Determines if the currently running sound manager is capable of supporting
- * multiple channels of sound.
- *
- * ON ENTRY: no preconditions
- * ON EXIT: Returns true if the currently running sound manager is capable
- * of supporting multiple channels of sound.
- *****************************************************************************/
-
- // DA on 01/20/96: public routines added
-
- extern OSErr
- CMSGetChannelVolume( short channelNum, unsigned short *volume );
- /*
- * Reads the current volume on the selected channel.
- *
- * ON ENTRY: 'channelNum' is a valid channel number.
- * 'volume' is a pointer to an unsigned short value.
- * ON EXIT: Returns 'noErr' if the volume was correctly read from the specified
- * channel; in this case, 'volume' contains a number between kMinVolume
- * and kMaxVolume.
- *****************************************************************************/
-
- extern OSErr
- CMSSetChannelVolume( short channelNum, unsigned short volume );
- /*
- * Assigns specified volume to a selected channel.
- *
- * ON ENTRY: 'channelNum' is a valid channel number.
- * 'volume' is a value between zero and 256.
- * ON EXIT: Returns 'noErr' if the volume was correctly assigned; in this case,
- * the new volume has been assigned to the selected channel.
- *****************************************************************************/
-
- extern OSErr
- CMSOpenSoundTool( short numChannels );
- /*
- * Initialize the SoundSystem and allocate all requested channels.
- *
- * ON ENTRY: 'numChannels' is the number of channels to be opened.
- * ON EXIT: Returns 'noErr' if the package has been correctly inited.
- *****************************************************************************/
-
- extern OSErr
- CMSPlaySoundPriority( short sound, short priority );
- /*
- * Play a sound (if possible) with a specified priority on any sound channel.
- *
- * ON ENTRY: 'sound' is the sound reference number to be played.
- * 'priority' is the desired priority of the sound to be played.
- * ON EXIT: Returns: 'noErr' if the sound was played on a free channel;
- * kPriorityOverride if the sound was played on a channel which
- * had lower priority; kPriorityDenied if the sound was not played
- * because all channels had a higher priority, or an error code
- * from the Sound Manager if there had been troubles with sound
- * playing.
- *****************************************************************************/
-
- extern OSErr
- CMSSetToolVolume( short volume );
- /*
- * Increment or decrement the volume on all open channels.
- *
- * ON ENTRY: 'volume' is positive if the volume has be to raised; negative
- * otherwise.
- * ON EXIT: Returns 'noErr' if the volume was successfully changed on all
- * open channels.
- *****************************************************************************/
-
-
- #endif /* __CMSOUNDSYSTEM__ */
-